package com.qust.enDecrpytion.proxy.status;

import com.qust.enDecrpytion.proxy.StatusHandleFunction;
import lombok.NoArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.annotation.Annotation;
@NoArgsConstructor
public class StringTypeHandle extends AbstractTypeHandle {
    private final Logger logger = LogManager.getLogger(StringTypeHandle.class);


    @Override
    public <T> T handle(T target,boolean cache ,Class<? extends Annotation> annotation, StatusHandleFunction function) {
        if (target.getClass() == String.class) {
            String lruKey = (String) target;
            if (cache){
                String value;
                if ((value = getFromCache(lruKey, annotation)) != null) {
                    logger.debug("内存命中");
                } else {
                    logger.debug("内存未命中");
                    value = function.enDecryption(lruKey);
                    add2Cache(lruKey, value, annotation);
                }
                return (T) value;
            }else {
                return (T) function.enDecryption(lruKey);
            }
        } else {
            return null;
        }
    }


}
